<?php
class Mailer
{
    /**
     * Función para enviar correos electrónicos usando php mailer
     * @param type $to
     * @param type $subject
     * @param type $body
     * @param type $from
     * @param type $fromName
     */
    public static function sendEmail($to, $subject, $body, $from = Mail::FROM, $fromName = Mail::FROM_NAME){
        if(Mail::EMAIL){
            Yii::app()->mailer->Host = Mail::HOST;
            Yii::app()->mailer->IsSMTP();
            Yii::app()->mailer->Port = Mail::PORT;
            Yii::app()->mailer->SMTPAuth = Mail::SMTP_AUTH;
            Yii::app()->mailer->SMTPSecure = Mail::SMTP_SECURE;
            Yii::app()->mailer->Username = Mail::USERNAME;
            Yii::app()->mailer->Password = Mail::PASSWORD;
            Yii::app()->mailer->From = $from;
            Yii::app()->mailer->FromName = $fromName;
            Yii::app()->mailer->AddReplyTo($from);
            Yii::app()->mailer->IsHTML(true);
            if(is_array($to)){
                foreach($to as $t){
                    Yii::app()->mailer->AddAddress($t);
                }                
            }else{
                Yii::app()->mailer->AddAddress($to);
            }
            Yii::app()->mailer->Subject = $subject;
            Yii::app()->mailer->Body = $body;
            if(!Yii::app()->mailer->Send()){
                throw new Exception('Error enviando el correo');
            }
        }
    }
}

?>
